package com.note.view;

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import javax.swing.*;

import com.note.bean.Note;
import com.note.dao.DerbyDB;
import com.note.services.NoteServices;
import com.note.util.JCalendarChooser;

public class MainWindow extends JFrame implements ComponentListener,
		WindowListener, FocusListener, ActionListener, MouseListener,KeyListener,AWTEventListener {
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	SimpleDateFormat sdfDateNoHaveTime = new SimpleDateFormat(
			"yyyy-MM-dd");
	JCalendarChooser jCalendarChooser=new JCalendarChooser(this);
	NoteServices noteServices;
	// 菜单
	JMenuBar jmb = new JMenuBar();
	JMenu jm = new JMenu("菜单");
	JMenu jm_file = new JMenu("文件");
	JMenuItem jmi_create = new JMenuItem("创建数据库");
	JMenuItem jmi_showDB = new JMenuItem("查看数据文件位置");
	JMenuItem jmi_save = new JMenuItem("保存");
	JMenuItem jmi_help = new JMenuItem("帮助");
	// 容器
	JPanel jp_main = new JPanel();
	JPanel jp_head = new JPanel();
	JPanel jp_myFeel = new JPanel(new FlowLayout(FlowLayout.LEFT));
	JPanel jp_mybody = new JPanel(new FlowLayout(FlowLayout.LEFT));
	GridBagLayout gridBagLayout = new GridBagLayout();
	JPanel jp_main_center = new JPanel(gridBagLayout);
	JScrollPane jsp_main_center;
	JPanel jp_work = new JPanel(gridBagLayout);
	JPanel jp_xinde = new JPanel(gridBagLayout);
	JPanel jp_zhichu = new JPanel(gridBagLayout);
	JPanel jp_zhichu_miaoshu = new JPanel(gridBagLayout);
	JPanel jp_daiban = new JPanel(gridBagLayout);

	// 组件
	JButton jb_toDay = new JButton("今天");
	JButton jb_yesterday = new JButton("昨天");
	JButton jb_beforeYesterday = new JButton("前天"); 
	JButton jb_jCalendarChooser = new JButton("日历");
	Date notedate=new Date();
	JLabel  jl_dateShow = new JLabel(sdfDateNoHaveTime.format(notedate));
	ButtonGroup bg_myfeel = new ButtonGroup();
	ButtonGroup bg_mybody = new ButtonGroup();
	JLabel jl_myfell = new JLabel(" 心 情 ： ");
	JLabel jl_kaixin;
	JRadioButton jcb_kaixin = new JRadioButton("开心", true);
	JLabel jl_yiban;
	JRadioButton jcb_yiban = new JRadioButton("一般");
	JLabel jl_daku;
	JRadioButton jcb_daku = new JRadioButton("伤心");
	// mybody
	JLabel jl_mybody = new JLabel(" 健 康 ： ");
	JRadioButton jrb_hao = new JRadioButton("好", true);
	JRadioButton jrb_yiban = new JRadioButton("一般");
	JRadioButton jrb_cha = new JRadioButton("差");
	final static String jta_work_defultText = "请输入今天的工作。。。";
	final static String jta_xinde_defultText = "请输入今天的心得体会。。。";
	final static String jta_zhichu_defultText = "请描述资金支出。。。";
	final static String jta_daiban_defultText = "请输入待办事项。。。";
	// 工作内容
	JLabel jl_work = new JLabel("工作内容：");
	JTextArea jta_work = new JTextArea(jta_work_defultText, 5, 20);
	// 心得体会
	JLabel jl_xinde = new JLabel("心得体会：");
	JTextArea jta_xinde = new JTextArea(jta_xinde_defultText, 5, 20);
	// 资金支出
	JLabel jl_zhichu = new JLabel("支出总额：");
	JTextField jtf_zhichu = new JTextField();
	JLabel jl_zhichu_miaoshu = new JLabel("支出描述：");

	JTextArea jta_zhichu = new JTextArea(jta_zhichu_defultText, 5, 20);
	// 待办内容
	JLabel jl_daiban = new JLabel("待办事项：");
	JTextArea jta_daiban = new JTextArea(jta_daiban_defultText, 5, 20);
	Image img_kaixin = this.getToolkit().getImage(
			MainWindow.class.getClassLoader().getResource(
					"images/kaixin.png"));
	Image img_yiban = this.getToolkit().getImage(
			MainWindow.class.getClassLoader().getResource(
					"images/yiban.png"));
	Image img_daku = this.getToolkit().getImage(
			MainWindow.class.getClassLoader()
					.getResource("images/daku.png"));
	// 构造方法
	public MainWindow() {
		try{
		noteServices = new NoteServices();
		this.setSize(1000,700);
		this.setTitle("我的日记本");
		menuInit();
		jPanelInit();
		addListenerInit();
		Note note = noteServices.getNoteByDate(new Date());
		noteInit(note);
		this.setVisible(true);
		
		this.addComponentListener(this);
		this.addWindowListener(this);
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "你已经启动了该程序了！"); 
			System.exit(0);
		}
	}

	// 菜单栏的初始化
	public void menuInit() {
		jm_file.add(jmi_create);
		jm_file.add(jmi_showDB);
		jm.add(jm_file);
		jm.addSeparator();
		jm.add(jmi_help);
		jmb.add(jm);
		jmb.add(jmi_save);
		// listener
		jmi_save.setActionCommand("jmi_save");
		jmi_save.addActionListener(this);
		jmi_create.setActionCommand("jmi_create");
		jmi_create.addActionListener(this);
		jmi_showDB.setActionCommand("jmi_showDB");
		jmi_showDB.addActionListener(this);
		this.setJMenuBar(jmb);
	}

	// 容器初始化
	public void jPanelInit() {
		jp_main.setLayout(new BorderLayout());
		// 头部容器初始化
		jp_head.setLayout(new FlowLayout(FlowLayout.LEFT));
		jl_dateShow.setForeground(Color.blue);
		jl_dateShow.setBackground(Color.green);
		jp_head.add(jl_dateShow);
		jp_head.add(jb_toDay);
		jp_head.add(jb_yesterday);
		jp_head.add(jb_beforeYesterday);
		jp_head.add(jb_jCalendarChooser);
		jb_toDay.setActionCommand("jb_toDay");
		jb_toDay.addActionListener(this); 
		jb_yesterday.setActionCommand("jb_yesterday");
		jb_yesterday.addActionListener(this); 
		jb_beforeYesterday.setActionCommand("jb_beforeYesterday");
		jb_beforeYesterday.addActionListener(this); 
		jb_jCalendarChooser.setActionCommand("jb_jCalendarChooser");
		jb_jCalendarChooser.addActionListener(this); 
		jp_head.setBorder(BorderFactory.createLineBorder(Color.BLUE, 1));
		// 图片初始化
		
		jl_kaixin = new JLabel(new ImageIcon(img_kaixin));
	
		jl_yiban = new JLabel(new ImageIcon(img_yiban));
		jl_daku = new JLabel(new ImageIcon(img_daku));
		jl_daku.addMouseListener(this);
		jl_yiban.addMouseListener(this);
		jl_kaixin.addMouseListener(this);
		// 我的心情 单选组 初始化
		bg_myfeel.add(jcb_daku);
		bg_myfeel.add(jcb_kaixin);
		bg_myfeel.add(jcb_yiban);
		jp_myFeel.add(jl_myfell);
		jp_myFeel.add(jcb_kaixin);
		jp_myFeel.add(jl_kaixin);
		jp_myFeel.add(jcb_yiban);
		jp_myFeel.add(jl_yiban);
		jp_myFeel.add(jcb_daku);
		jp_myFeel.add(jl_daku);
		jp_myFeel.setBorder(BorderFactory.createLineBorder(Color.ORANGE, 1));
		// 我的身体
		jp_mybody.add(jl_mybody);
		bg_mybody.add(jrb_hao);
		bg_mybody.add(jrb_yiban);
		bg_mybody.add(jrb_cha);
		jp_mybody.add(jrb_hao);
		jp_mybody.add(jrb_yiban);
		jp_mybody.add(jrb_cha);
		// 工作内容
		jp_work.add(jl_work);
		jp_work.add(jta_work);
		// 心得体会
		jp_xinde.add(jl_xinde);
		jp_xinde.add(jta_xinde);
		// 资金支出
		jp_zhichu.add(jl_zhichu);
		jp_zhichu.add(jtf_zhichu);
		jp_zhichu.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 1));
		jp_zhichu_miaoshu.add(jl_zhichu_miaoshu);
		jp_zhichu_miaoshu.add(jta_zhichu);
		jp_zhichu_miaoshu.setBorder(BorderFactory.createLineBorder(
				Color.YELLOW, 1));
		// 待办事项
		jp_daiban.add(jl_daiban);
		jp_daiban.add(jta_daiban);
		// 主容器
		jp_main_center.add(jp_myFeel);
		jp_main_center.add(jp_mybody);
		jp_main_center.add(jp_work);
		jp_main_center.add(jp_xinde);
		jp_main_center.add(jp_zhichu);
		jp_main_center.add(jp_zhichu_miaoshu);
		jp_main_center.add(jp_daiban);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(4, 4, 4, 4);
		gbc.fill = GridBagConstraints.BOTH;
		// 工作P L TEXTA 位置
		gbc.gridwidth = 1;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jl_work, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jta_work, gbc);
		jp_work.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 1));
		// 心得P L TEXTA 位置
		gbc.gridwidth = 1;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jl_xinde, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jta_xinde, gbc);
		jp_xinde.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 1));
		// 资金支出 位置
		gbc.gridwidth = 1;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jl_zhichu, gbc);
		gbc.gridwidth = 0;
		gbc.gridheight = 5;
		gbc.weightx = 40.0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jtf_zhichu, gbc);
		gbc.gridwidth = 1;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jl_zhichu_miaoshu, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gridBagLayout.setConstraints(jta_zhichu, gbc);
		// 待办事项的L JTA位置
		gbc.gridwidth = 1;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jl_daiban, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jta_daiban, gbc);
		jp_daiban.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 1));
		// jp_main_center jp_myFeel jp_work 位置
		gbc.gridwidth = 0;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jp_myFeel, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 0;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jp_mybody, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jp_work, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jp_xinde, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jp_zhichu, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jp_zhichu_miaoshu, gbc);
		gbc.gridwidth = 0;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gridBagLayout.setConstraints(jp_daiban, gbc);

		jp_main.add(jp_head, BorderLayout.NORTH);
		jsp_main_center=new JScrollPane(jp_main_center);
		jp_main.add(jsp_main_center, BorderLayout.CENTER);
		this.add(jp_main);
	}

	// 监听器初始化
	public void addListenerInit() {
		jta_zhichu.addFocusListener(this);
		jta_xinde.addFocusListener(this);
		jta_work.addFocusListener(this);
		jta_daiban.addFocusListener(this);
		Toolkit tk = Toolkit.getDefaultToolkit();  
        tk.addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);  
	}

	public void noteInit(Note note) { 
		
		System.out.println("noteDBInit="+note);
		if (note == null) {
			jtf_zhichu.setText("");
			jta_work.setText("");
			jta_xinde.setText("");
			jta_zhichu.setText("");
			jta_daiban.setText("");
			jcb_kaixin.setSelected(true);
			jrb_hao.setSelected(true);
			return;
		}
		
		String feel = note.getFeel();
		String body = note.getBody();
		System.out.println(feel);
		if ("kx".equals(feel)) {
			jcb_kaixin.setSelected(true);
		}else if("yb".equals(feel)){
			jcb_yiban.setSelected(true);
		}else if("sx".equals(feel)){
			jcb_daku.setSelected(true);
		} 
		if ("ha".equals(body)) {
			jrb_hao.setSelected(true);
		}else if("yb".equals(body)){
			jrb_yiban.setSelected(true);
		}else if("ch".equals(body)){
			jrb_cha.setSelected(true);
		} 
		jtf_zhichu.setText(note.getZhichujine());
		jta_work.setText(note.getWork());
		jta_xinde.setText(note.getXinde());
		jta_zhichu.setText(note.getZhichumiaoshu());
		jta_daiban.setText(note.getDaiban());

	}

	public static void main(String[] args) {
		new MainWindow();
		// System.out.println(NoteServices.class.getClassLoader().getResource("").getPath());
	}

	public Note getNote() {
		Note note = new Note(); 
		note.setDate(notedate);
		note.setStringdate(simpleDateFormat.format(notedate));
		note.setWork(jta_work.getText());
		note.setXinde(jta_xinde.getText());
		note.setZhichujine(jtf_zhichu.getText());
		note.setZhichumiaoshu(jta_zhichu.getText());
		note.setDaiban(jta_daiban.getText());
		String myfeel = "yb";
		if (jcb_daku.isSelected()) {
			myfeel = "sx";
		} else if (jcb_yiban.isSelected()) {
			myfeel = "yb";
		} else if (jcb_kaixin.isSelected()) {
			myfeel = "kx";
		}
		String mybody = "yb";
		if (jrb_hao.isSelected()) {
			mybody = "ha";
		} else if (jrb_yiban.isSelected()) {
			mybody = "yb";
		} else if (jrb_cha.isSelected()) {
			mybody = "ch";
		}
		System.out.println(myfeel+"getnote");
		note.setFeel(myfeel);
		note.setBody(mybody);
		return note;
	}

	@Override
	public void componentResized(ComponentEvent e) {
	}

	public void componentMoved(ComponentEvent e) {
	}

	public void componentShown(ComponentEvent e) {
	}

	@Override
	public void componentHidden(ComponentEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}

	@Override
	public void windowClosed(WindowEvent e) {
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}

	@Override
	public void focusGained(FocusEvent e) {
		JTextArea jta = (JTextArea) e.getComponent();
		String jtaText = jta.getText();
		if (jta_work_defultText.equals(jtaText)
				|| jta_xinde_defultText.equals(jtaText)
				|| jta_zhichu_defultText.equals(jtaText)||jta_daiban_defultText.equals(jtaText)) {
			jta.setText("");
		}
		if ("请输入。。。".equals(jtaText)) {
			jta.setText("");
		}

	}

	@Override
	public void focusLost(FocusEvent e) {
		JTextArea jta = (JTextArea) e.getComponent();
		String jtaText = jta.getText();
		if ("".equals(jtaText)) {
			jta.setText("请输入。。。");
		}

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String ac=e.getActionCommand();
		if ("jmi_save".equals(e.getActionCommand())) {
			Note note = getNote();
			noteServices.saveOrUpDateNote(note);
			
			JOptionPane jOptionPane=new JOptionPane("保存成功"); 
			jOptionPane.setIcon(new ImageIcon(img_kaixin));
			final JDialog jd= jOptionPane.createDialog(this, "消息：");
			new Thread(new Runnable() { 
				public void run() { 
					try {
						Thread.sleep(1500);
						jd.setVisible(false);
						jd.dispose();
					} catch (InterruptedException e) { 
						e.printStackTrace();
					}
				}
			}).start();;
			jd.setVisible(true);  
			
			
		} else if ("jmi_create".equals(e.getActionCommand())) {
			int result=noteServices.creatTable_Note();
			if (result<0) {
				JOptionPane.showMessageDialog(this, "创建数据库失败！请检查是否已存在数据库！");
			}else {
				JOptionPane.showMessageDialog(this, "创建数据库成功！");
			}
		   
		}else if("jmi_showDB".equals(e.getActionCommand())){
			JOptionPane.showMessageDialog(this, "数据库文件地址：\n"+DerbyDB.dbName);
			
		}else if ("jb_jCalendarChooser".equals(e.getActionCommand())) {
			Calendar calendar=jCalendarChooser.showCalendarDialog();
			notedate=calendar.getTime();
			Note note = noteServices.getNoteByDate(notedate);
			jl_dateShow.setText(sdfDateNoHaveTime.format(notedate));
			noteInit(note);
		}else if ("jb_toDay".equals(ac)) { 
			notedate=new Date();
			Note note = noteServices.getNoteByDate(notedate);
			jl_dateShow.setText(sdfDateNoHaveTime.format(notedate));
			noteInit(note); 
		}else if ("jb_yesterday".equals(ac)) { 
			Calendar calendar=Calendar.getInstance();
			calendar.add(Calendar.DATE, -1);
			notedate=calendar.getTime();
			Note note = noteServices.getNoteByDate(notedate);
			jl_dateShow.setText(sdfDateNoHaveTime.format(notedate));
			noteInit(note); 
		}else if ("jb_beforeYesterday".equals(ac)) { 
			Calendar calendar=Calendar.getInstance();
			calendar.add(Calendar.DATE, -2);
			notedate=calendar.getTime();
			Note note = noteServices.getNoteByDate(notedate);
			jl_dateShow.setText(sdfDateNoHaveTime.format(notedate));
			noteInit(note); 
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		Component c = e.getComponent();
		if (c == jl_daku) {
			jcb_daku.setSelected(true);
		} else if (c == jl_kaixin) {
			jcb_kaixin.setSelected(true);
		} else if (c == jl_yiban) {
			jcb_yiban.setSelected(true);
		}

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) { 
		System.out.println("keyTyped");
	}

	@Override
	public void keyPressed(KeyEvent e) { 
	} 
	@Override
	public void keyReleased(KeyEvent e) { 
		
	}

	@Override
	public void eventDispatched(AWTEvent event) {
        if (event.getClass() == KeyEvent.class) {  
            // 被处理的事件是键盘事件.  
            KeyEvent keyEvent = (KeyEvent) event;  
            if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {   
            	if (keyEvent.isControlDown()&&keyEvent.getKeyCode()==KeyEvent.VK_S) { 
        			jmi_save.doClick();
        		}
            	if (keyEvent.isMetaDown()&&keyEvent.getKeyCode()==KeyEvent.VK_S) { 
        			jmi_save.doClick();
        		}
                keyPressed(keyEvent);  
            } else if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {   
                keyReleased(keyEvent);  
            }  
        } 
		
	}
}
